home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 October / CHIP Turkiye Ekim 2000.iso / prog / naps / 04 / setup.exe / Gnucleus / GnuSock.h < prev    next >
C/C++ Source or Header  |  2000-07-15  |  5KB  |  189 lines

  1. #if !defined(AFX_GNUSOCK_H__60210B41_1340_11D4_ACF2_00A0CC533D52__INCLUDED_)
  2. #define AFX_GNUSOCK_H__60210B41_1340_11D4_ACF2_00A0CC533D52__INCLUDED_
  3.  
  4. #if _MSC_VER > 1000
  5. #pragma once
  6. #endif // _MSC_VER > 1000
  7. // GnuSock.h : header file
  8. //
  9.  
  10. #include <queue>
  11. class QueryResult;
  12. class CViewSearch ;
  13.  
  14. class QueryRequestItem
  15. {
  16. public:
  17.     QueryRequestItem (LPCTSTR szQuery, const GUID& guid) : m_query (szQuery), m_guid (guid) { }
  18.     QueryRequestItem (const QueryRequestItem& copy) : m_query (copy.m_query), m_guid (copy.m_guid) { }
  19.     virtual ~QueryRequestItem () { }
  20.  
  21.     const CString& GetQuery () const { return m_query; }
  22.     const GUID& GetGuid () const { return m_guid; }
  23. protected:
  24.     CString m_query;
  25.     GUID m_guid;
  26. };
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CGnuSock command target
  30.  
  31. // Seperate thread
  32.  
  33. class CGnuSock : public CAsyncSocket
  34. {
  35. // Attributes
  36. public:
  37.     DWORD        m_dwNumOfBadPackets;        // counters
  38.     DWORD        m_dwNumOfRecivedPackets;// Good packets
  39.     DWORD        m_dwNumOfSentPackets;
  40.  
  41. // Operations
  42. public:
  43.     CGnuSock();
  44.     CGnuSock(CGnuControl *pGnuComm);
  45.     virtual ~CGnuSock();
  46.  
  47.     void Send_ConnectOK();
  48.     void Send_Ping();
  49.     void Send_Pong(GUID, int);
  50.     void Send_Query(CViewSearch *);
  51.  
  52.     void Recieve_ConnectOK();
  53.     void Recieve_Ping(packet_Ping *, int);
  54.     void Recieve_Pong(packet_Pong *, int);
  55.     void Recieve_Push(packet_Push *, int);
  56.     void Recieve_Query(packet_Query *, int);
  57.     void Recieve_QueryReply(packet_QueryReply *, DWORD);
  58.     void Recieve_Unknown(byte *, int);
  59.  
  60.     bool SpaceAvailable();
  61.     int  SplitBundle(byte *, DWORD);
  62.     void HandlePacket(packet_Header *, int);
  63.     bool InspectPacket(packet_Header *, int);
  64.     void CleanUp();
  65.  
  66.     // Remote hosts array, elements represent hop lengths
  67.     DWORD RemoteHosts[8];
  68.  
  69.     void  RefreshHostCount();
  70.     DWORD TotalRemoteHosts();
  71.  
  72.     // Log string and options of what to append to it
  73.     CString Log;
  74.     bool logPings, logPongs, logQueries,
  75.          logPushes, logQueryReplies, logUnknowns,
  76.          logGUID, logHops, logTTL, logPayload, logDropped;
  77.  
  78.     // Remote host and port of connnection
  79.     CString Host, Port;
  80.     bool Connected;
  81.  
  82.     // Current search uses these variables
  83.     CString     CurrentSearch;
  84.     GUID        CurrentOrigin;
  85.  
  86.     bool m_CanRelease;
  87.     
  88.     // Link to next CGnuSock node in the list
  89.     CGnuSock    *next;
  90.     CGnuControl *GnuComm;
  91. // Overrides
  92. public:
  93.     BOOL ProccessPacketQueue();
  94.     virtual int Send( const void* lpBuf, int nBufLen, int nFlags = 0 );
  95.     DWORD m_dwBytesIn;        // for one period only
  96.     DWORD m_dwBytesOut;        // for one period only
  97.     DWORD m_dwTotalBytesIn;
  98.     DWORD m_dwTotalBytesOut;
  99.     DWORD m_dwByteAllottmentOut;
  100.     DWORD m_dwByteAllottmentIn;
  101.     int m_iSizeOfPacketQueue;
  102.     void SendFileResults(std::queue <QueryResult> & resultQueue, GUID & Origin);
  103.  
  104.     DWORD GetSecondsSinceLastContact(void)    
  105.         {return ( CTime::GetCurrentTime() - m_timeLastHeardAT ).GetTotalSeconds();}
  106.  
  107.     BOOL ShutDown(int nHow)
  108.     { 
  109.         if(Connected)
  110.         { 
  111.             Connected = false; 
  112.             return (SOCKET_ERROR != shutdown(m_hSocket,nHow)); 
  113.         }
  114.         else 
  115.             return FALSE; 
  116.     }
  117.  
  118.     void WantToDisconnect();
  119.  
  120.     // ClassWizard generated virtual function overrides
  121.     //{{AFX_VIRTUAL(CGnuSock)
  122.     public:
  123.     virtual void OnConnect(int nErrorCode);
  124.     virtual void OnReceive(int nErrorCode);
  125.     virtual void OnClose(int nErrorCode);
  126.     //}}AFX_VIRTUAL
  127.  
  128.     // Generated message map functions
  129.     //{{AFX_MSG(CGnuSock)
  130.         // NOTE - the ClassWizard will add and remove member functions here.
  131.     //}}AFX_MSG
  132.  
  133. // Implementation
  134. protected:
  135.     CCriticalSection    m_CritSecPacketQueue;
  136.     std::queue<void*>    m_PacketsOutQueue;
  137.     std::queue<int>        m_PacketsOutQueueSize;
  138.  
  139.     void        Initialize ();
  140.  
  141.     CTime        m_timeLastHeardAT;        // hold time of last contact, for expiring non responding hosts.
  142.  
  143.     int            mExtraBytes;
  144.     byte       *mExtraBytesStored;
  145. };
  146.  
  147. class QueryResult
  148. {
  149. public:
  150.     QueryResult (DWORD dwIndex, DWORD dwSize, CString& szName) : m_index (dwIndex), m_size (dwSize), m_name (szName) { }
  151.     QueryResult (const QueryResult& copy) : m_index (copy.m_index), m_size (copy.m_size), m_name (copy.m_name) { }
  152.     virtual ~QueryResult () { }
  153.  
  154.     inline DWORD GetSize () const { return m_name.GetLength () + 10; }
  155.  
  156.     void CopyToBuffer (BYTE* buffer, DWORD dwSize) const
  157.     {
  158.         ASSERT (dwSize >= GetSize ());
  159.  
  160.         * (XWORD*) buffer = flipX (makeX (m_index));
  161.         buffer += sizeof (XWORD);
  162.  
  163.         * (XWORD*) buffer = flipX (makeX (m_size));
  164.         buffer += sizeof (XWORD);
  165.  
  166.         ::strcpy ((char*) buffer, m_name);
  167.  
  168.         buffer += m_name.GetLength() + 1;
  169.  
  170.         *buffer = '\0';
  171.     }
  172.  
  173. protected:
  174.     DWORD m_index;
  175.     DWORD m_size;
  176.     CString m_name;
  177.  
  178.     // Not allowed
  179.     QueryResult ();
  180. };
  181.  
  182.  
  183. /////////////////////////////////////////////////////////////////////////////
  184.  
  185. //{{AFX_INSERT_LOCATION}}
  186. // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
  187.  
  188. #endif // !defined(AFX_GNUSOCK_H__60210B41_1340_11D4_ACF2_00A0CC533D52__INCLUDED_)
  189.